﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cnFederal
{
    public class FaPosts
    {
         private static CnfiDataDataContext lnq;
         private static List<FaPost> _postList;
          public FaPosts()
        {
            
           
        }

          public List<FaPost> PostList
          {
              get
              {
                  return _postList;
              }
              set
              {
                  _postList = value;
              }
          }

          public static List<FaPost> GetArticlesFromDateToDate()
          {
              var onlyArticles = from c in GetAllPostsFromDateToDate()
                                 where c.NewsType == 2 &&
                                 (c.Category == "GeneralArticle" || c.Category == "Articles")
                                 select c;
              return onlyArticles.ToList();
          }

          public static List<FaPost> GetAllPostsFromDateToDate()
          {
              List<FaPost> liste = new List<FaPost>();
              if (_postList != null)
              {
                  foreach (FaPost post in _postList)
                  {
                   if(post.DateStartPublish <= DateTime.Now && 
                      post.DateEndPublish >= DateTime.Now)
                      liste.Add(post);
                  }

              }

              return Enumerable.Reverse(liste).ToList();
          }

      

          public static void Load(bool display = true)
          {
              lnq = new CnfiDataDataContext();
              if (_postList == null)
              _postList = new List<FaPost>();
              if (_postList != null)
              {
                  List<FaPost> liste = new List<FaPost>();
                  var b = from c in lnq.FaNews
                          where c.Display == display
                          orderby c.Id
                          select c;

                  foreach (var s in b)
                  {
                      FaPost news = new FaPost();
                      news.Id = (int)s.Id;
                      news.Header = (string)s.Header;
                      news.MainContent = (string)s.MainContent;
                      news.Title = (string)s.Title;
                      news.ImageUrl = (string)s.ImageUrl;
                      if(s.AuthorId != null)
                      news.AuthorId = (int)s.AuthorId;
                      if(s.DateEndPublish != null)
                      news.DateEndPublish = (DateTime)s.DateEndPublish;
                      if(s.DateRegistered != null)
                      news.DateRegistered = (DateTime)s.DateRegistered;
                      if(s.DateStartPublish != null)
                      news.DateStartPublish = (DateTime)s.DateStartPublish;
                      if(s.DateUpdated != null)
                      news.DateUpdated = (DateTime)s.DateUpdated;
                      if(s.userId != null)
                      news.UserId = (int)s.userId;
                      if (s.Category != null)
                      news.Category = (string)s.Category;
                      if(s.Display != null)
                      news.Display = (bool)s.Display;
                      if(s.Viewed != null)
                      news.Viewed = (int)s.Viewed;
                      if (s.NewsType != null)
                          news.NewsType = (int)s.NewsType;
                      liste.Add(news);
                      
                  }

                  _postList = liste;
              }
          }

          public void RefreshPostlist()
          {
              _postList.Clear();
              _postList = GetAllFaPosts();
          }
        
          public static List<FaPost> GetAllFaPosts()
          {

              return Enumerable.Reverse(_postList).ToList();
          }
          public static List<FaPost> GeReversetAllFaPosts()
          {
              var b = from c in _postList
                               orderby c.Id
                               select c;
              return Enumerable.Reverse(b).ToList();
          }
        

          public static List<FaPost> GetTopNews(int number)
          {
              if (GetAllPostsFromDateToDate() == null) return null;

              var onlyNews = from c in GetAllPostsFromDateToDate()
                            where c.NewsType == 1
                            select c;
              return onlyNews.Take(number).ToList();
          }

          public static List<FaPost> GetFaPostsByCategory(string category, bool display = true)
          {
              if (_postList == null) return null;
              List<FaPost> list = new List<FaPost>();
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              var b = from c in tempList
                      where c.Category == category && c.Display == display
                      select c;
              foreach (var s in b)
              {
                  FaPost news = new FaPost();
                  news.Id = (int)s.Id;
                  news.Header = (string)s.Header;
                  news.MainContent = (string)s.MainContent;
                  news.Title = (string)s.Title;
                  news.ImageUrl = (string)s.ImageUrl;
                  if (s.AuthorId != null)
                      news.AuthorId = (int)s.AuthorId;
                  if (s.Category != null)
                      news.Category = (string)s.Category;
                  if (s.Display != null)
                      news.Display = (bool)s.Display;
                  if (s.Viewed != null)
                      news.Viewed = (int)s.Viewed;
                  if (s.NewsType != null)
                      news.NewsType = (int)s.NewsType;
                  news.DateRegistered = (DateTime)s.DateRegistered;
                  news.DateStartPublish = (DateTime)s.DateStartPublish;
                  news.DateEndPublish = (DateTime)s.DateEndPublish;
                  list.Add(news);
              }

              return list;
          }

          public static List<FaPost> GetFaPostsByType(int type, bool display = true)
          {
              if (_postList == null) return null;
              List<FaPost> list = new List<FaPost>();
              List<FaPost> tempList = GetAllPostsFromDateToDate();
             
              var b = from c in tempList
                      where c.NewsType == type && c.Display == display
                      select c;
              if(type == 1)//Get both news and shortNews.
               b = from c in tempList
                      where c.NewsType == type || c.NewsType == 7 && c.Display == display
                      select c;

              foreach (var s in b)
              {
                  FaPost news = new FaPost();
                  news.Id = (int)s.Id;
                  news.Header = (string)s.Header;
                  news.MainContent = (string)s.MainContent;
                  news.Title = (string)s.Title;
                  news.ImageUrl = (string)s.ImageUrl;
                  if (s.AuthorId != null)
                      news.AuthorId = (int)s.AuthorId;
                  if (s.Category != null)
                      news.Category = (string)s.Category;
                  if (s.Display != null)
                      news.Display = (bool)s.Display;
                  if (s.Viewed != null)
                      news.Viewed = (int)s.Viewed;
                  if (s.NewsType != null)
                      news.NewsType = (int)s.NewsType;
                  news.DateRegistered = (DateTime)s.DateRegistered;
                  news.DateStartPublish = (DateTime)s.DateStartPublish;
                  news.DateEndPublish = (DateTime)s.DateEndPublish;
                  list.Add(news);
              }

              return list;
          }

          public static List<FaPost> GetLatinShortNewsByNymber(int number)
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              List<FaPost> posts = (from c in tempList
                                    where c.NewsType == 10 && c.Display == true
                                    select c).OrderBy(a => a.Id).Reverse().Take(number).ToList();

              return posts;
          }


        /// <summary>
        /// Return a list of FaPost, count = number.
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
          public static List<FaPost> GetShortNewsByNymber(int number)
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              List<FaPost> posts = (from c in tempList
                       where c.NewsType == 7 && c.Display == true
                       select c).OrderBy(a =>a.Id).Reverse().Take(number).ToList();
             
              return posts;
          }

          public static FaPost GetTheLasInterview()
           {
               List<FaPost> tempList = GetAllPostsFromDateToDate();
               if (tempList == null) return null;
               FaPost post = null;
               var b = (from c in tempList
                       where c.NewsType == 5 && c.Display == true 
                       select c);
               if (b.Count() != 0) post = b.Last();
               return post;               
            }

          public static List<FaPost> GetLastInterviews(int number)
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              List<FaPost> posts = (from c in tempList
                                    where c.NewsType == 5 && c.Display == true 
                                    select c).OrderBy(a => a.Id).Reverse().Take(number).ToList();
              return posts;
          }

          public static List<FaPost> GetLastActivities(int number)
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              List<FaPost> posts = (from c in tempList
                                    where c.NewsType == 6 && c.Display == true
                                    select c).OrderBy(a => a.Id).Reverse().Take(number).ToList();
              return posts;

          }
          public static string CustomTitle
          {
              get
              {
                  List<FaPost> p = GetLastCustomPosts(3);
                  if (p != null && p.Count > 0)
                      return p[0].Title;
                  return null;
              }
          }
          public static List<FaPost> GetLastCustomPosts(int number)
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              List<FaPost> posts = (from c in tempList
                                    where c.NewsType == 8 && c.Display == true
                                    select c).OrderBy(a => a.Id).Reverse().Take(number).ToList();
              return posts;
          }

          public static FaPost GetTheLasActivity()
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              FaPost post = null;
              var b = (from c in tempList
                       where c.NewsType == 6 && c.Display == true
                       select c);
              if (b.Count() != 0) post = b.Last();
              return post;
          }

          public static FaPost GetLastFederalismPost()
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              FaPost post = null;
              var b = (from c in tempList
                       where c.Category != null && c.Category.Trim().ToLower() == "federalism" && c.Display == true
                       select c);
              if (b.Count() != 0) post = b.Last();
              return post;
          }
        /// <summary>
        /// Return number of posts by category like federalism.
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
          public static List<FaPost> GetLastFederalismPosts(int number)
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              List<FaPost> posts = (from c in tempList
                                    where c.Category != null && c.Category.Trim().ToLower() == "federalism" && c.NewsType == 9 && c.Display == true 
                                    select c).OrderBy(a => a.Id).Reverse().Take(number).ToList();
              return posts;
          }

          public static FaPost GetFaPostById(int id)
          {
              List<FaPost> tempList = GetAllPostsFromDateToDate();
              if (tempList == null) return null;
              FaPost news = null;
              var b = from c in tempList
                      where c.Id == id
                      select c;
              foreach (var s in b)
              {
                   news = new FaPost();
                  news.Id = (int)s.Id;
                  news.Header = (string)s.Header;
                  news.MainContent = (string)s.MainContent;
                  news.Title = (string)s.Title;
                  news.ImageUrl = (string)s.ImageUrl;
                  if(s.AuthorId != null)
                  news.AuthorId = (int)s.AuthorId;
                  if (s.Category != null)
                  news.Category = (string)s.Category;
                  if (s.Display != null)
                  news.Display = (bool)s.Display;
                  if (s.Viewed != null)
                  news.Viewed = (int)s.Viewed;
                  if (s.NewsType != null)
                      news.NewsType = (int)s.NewsType;
                  news.DateRegistered = (DateTime)s.DateRegistered;
                  news.DateStartPublish = (DateTime)s.DateStartPublish;
                  news.DateEndPublish = (DateTime)s.DateEndPublish;
              }

              return news;
          }

          
          public static int CreateNew(FaPost f)
          {
              int lastId = 0;
              try
              {
                  FaNew fa = new FaNew();
                  fa.AuthorId = f.AuthorId;
                  fa.DateEndPublish = f.DateEndPublish;
                  fa.DateRegistered = f.DateRegistered;
                  fa.DateStartPublish = f.DateStartPublish;
                  fa.DateUpdated = f.DateUpdated;
                  fa.Header = f.Header;
                  fa.ImageUrl = f.ImageUrl;
                  fa.MainContent = f.MainContent;
                  fa.Title = f.Title;
                  fa.userId = f.UserId;
                  fa.Category = (string)f.Category;
                  fa.Display = (bool)f.Display;
                  fa.Viewed = (int)f.Viewed;
                  fa.NewsType = (int)f.NewsType;
                  lnq.FaNews.InsertOnSubmit(fa);
                  lnq.SubmitChanges();
                  lnq.Connection.Close();

                  lastId = (int)(from c in lnq.Links
                                 select c.Id).Max();
                  
              }
              catch (Exception ex)
              {
              }
              return lastId;
          }

          public static bool DeleteItem(int id)
          {
              bool deleted = false;
              var b = from f in lnq.FaNews
                      where f.Id == id
                      select f;
              if (b != null && b.Count() > 0)
              {
                  FaNew n = b.First();
                  try
                  {
                      lnq.FaNews.DeleteOnSubmit(n);
                      lnq.SubmitChanges();
                      deleted = true;
                  }
                  catch (Exception ex)
                  {
                      deleted = false;
                  }
              }
              return deleted;
          }
          public static void Save(FaPost fa)
          {
              var b = from f in lnq.FaNews
                      where f.Id == fa.Id
                      select f;
              foreach (var n in b)
              {
                  n.AuthorId = fa.AuthorId;
                  n.DateEndPublish = fa.DateEndPublish;
                  n.DateStartPublish = fa.DateStartPublish;
                  n.DateUpdated = fa.DateUpdated;
                  n.Header = fa.Header;
                  n.ImageUrl = fa.ImageUrl;
                  n.MainContent = fa.MainContent;
                  n.Title = fa.Title;
                  n.userId = fa.UserId;
                  n.Category = fa.Category;
                  n.Display = fa.Display;
                  n.Viewed = fa.Viewed;
                  n.NewsType = fa.NewsType;
              }
              lnq.SubmitChanges();
          }


    }

   public class FaPost
    {
        public int Id
        {
            get;
            set;
        }
        public int UserId
        {
            get;
            set;
        }
        public int AuthorId
        {
            get;
            set;
        }
        public string Header
        {
            get;
            set;
        }

        public string MainContent
        {
            get;
            set;
        }
        public string Title
        {
            get;
            set;
        }
        public string ImageUrl
        {
            get;
            set;
        }
        public DateTime DateRegistered
        {
            get;
            set;
        }
        public DateTime DateStartPublish
        {
            get;
            set;
        }
        public DateTime DateEndPublish
        {
            get;
            set;
        }
        public DateTime DateUpdated
        {
            get;
            set;
        }
        public string Category
        {
            get;
            set;
        }
        public bool Display
        {
            get;
            set;
        }

        public int Viewed
        {
            get;
            set;
        }
        public int NewsType
        {
            get;
            set;
        }
    }
}